
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "core/client/Sender.h"
#include "core/common/Packet.h"
#include "network/Socket.h"

Sender::Sender(Socket* socket,bool* isPilot)
    : AbstractThread(), pSocket_(socket)
{
    isPilot_ = isPilot;
}

Sender::~Sender()
{
}

void Sender::run()
{
    uint8_t buffer[Protocol::maxPacketSize - Protocol::minPacketSize] = {0};
    char temp[Protocol::minPacketSize] = {0};
    int w;

    do
    {
        std::cout<<"--------------- Menu ---------------"<<std::endl;
        std::cout<<"Pour une demande de controle : tapez 0"<<std::endl;
        std::cout<<"Pour relacher le controle : tapez 2"<<std::endl;
        std::cout<<"Pour deplacer le device : tapez 4"<<std::endl;
        std::cout<<"--------------- Menu ---------------"<<std::endl;
        std::cout<<"Entrez votre choix : ";
        std::cin>>temp;
        buffer[0] = atoi(&(temp[0]));

        switch(buffer[0])
        {
        case Protocol::request:
            w = pSocket_->write(&(buffer[0]),Protocol::minPacketSize);
            if (w < 0)
            {
                std::cerr << "> Could not write in " << pSocket_->interface() << " : " << std::endl;
                std::cerr << "> " << pSocket_->errorString() << std::endl;
            }
            if (w == 0)
            {
                std::cout << "> " << pSocket_->interface() << " had disconnected" << std::endl;
                pSocket_->close();
            }
            break;

        case Protocol::release:
            w = pSocket_->write(&(buffer[0]),Protocol::minPacketSize);
            if (w > 0)
            {
                *isPilot_ == false;
            }
            if (w < 0)
            {
                std::cerr << "> Could not write in " << pSocket_->interface() << " : " << std::endl;
                std::cerr << "> " << pSocket_->errorString() << std::endl;
            }
            if (w == 0)
            {
                std::cout << "> " << pSocket_->interface() << " had disconnected" << std::endl;
                pSocket_->close();
            }
            break;

        case Protocol::command:
            if (*isPilot_ == false)
            {
                std::cout<<"Vous n'etes pas le pilote du device !"<<std::endl;
            }
            else
            {
                std::cout<<"--------------- Commande ---------------"<<std::endl;
                std::cout<<"Pour deplacer le device vers le haut : tapez 5"<<std::endl;
                std::cout<<"Pour deplacer le device vers le bas : tapez 6"<<std::endl;
                std::cout<<"Pour deplacer le device vers la droite : tapez 7"<<std::endl;
                std::cout<<"Pour deplacer le device vers la gauche : tapez 8"<<std::endl;
                std::cout<<"--------------- Commande ---------------"<<std::endl;
                std::cout<<"Entrez votre commande : ";
                std::cin>>temp;

                if(strcmp(&(temp[0]),"5") == 0 || strcmp(&(temp[0]),"6") == 0 || strcmp(&(temp[0]),"7") == 0 || strcmp(&(temp[0]),"8") == 0)
                {
                    buffer[1] = atoi(&(temp[0]));
                    w = pSocket_->write(&buffer,Protocol::maxPacketSize - Protocol::minPacketSize);
                    if (w < 0)
                    {
                        std::cerr << "> Could not write in " << pSocket_->interface() << " : " << std::endl;
                        std::cerr << "> " << pSocket_->errorString() << std::endl;
                    }
                    if (w == 0)
                    {
                        std::cout << "> " << pSocket_->interface() << " had disconnected" << std::endl;
                        pSocket_->close();
                    }
                }
                else
                {
                    std::cout<<"Commande inconnu !"<<std::endl;
                }
            }
            break;

        default:
            std::cout<<"Choix inconnu !"<<std::endl;
            break;

        }


    } while(pSocket_->isOpen()); // On vérifie que le serveur ne s'est pas déconnecté
}
